package com.example.sunsg.okhttptest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.customview.PullToZoonListView;
import com.example.sunsg.okhttptest.customview.ScrollLinearlayoutView;

/**
 * @ Description 下拉显示图片
 * @ author sunsg
 * @ time 2015.10.24
 */
public class PullToZoonActivity extends Activity implements View.OnClickListener{
    private PullToZoonListView mPullToZoonListView;
    private ScrollLinearlayoutView mScrollLinearlayoutView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_to_zoon);

        //listview
        mPullToZoonListView = ((PullToZoonListView) findViewById(R.id.pull_listview));
        mPullToZoonListView.setAdapter(new MyAdapter(this));
        addHeadVeiw();
        mPullToZoonListView.setSelection(10);

        //linearylayt
        mScrollLinearlayoutView = ((ScrollLinearlayoutView) findViewById(R
                .id.scroll_linearlayout));
        addScrollLinearlayoutView();

        Button btnPullTOList = ((Button) findViewById(R.id.btn_pull_to_list));
        btnPullTOList.setOnClickListener(this);
        Button btnScrollLinearlayout = ((Button) findViewById(R.id.btn_scroll_linearlayout));
        btnScrollLinearlayout.setOnClickListener(this);
    }

    private void addHeadVeiw(){
        View view = LayoutInflater.from(this).inflate(R.layout.view_pull_to_zoon_listview_headview_child,null);
        mPullToZoonListView.addCustomView(view);
        Button left = ((Button) view.findViewById(R.id.btn_left));
        left.setOnClickListener(this);
        Button right = ((Button) view.findViewById(R.id.btn_right));
        right.setOnClickListener(this);
        Button all = ((Button) view.findViewById(R.id.btn_all));
        all.setOnClickListener(this);
    }

    private void addScrollLinearlayoutView(){
        mScrollLinearlayoutView.setOrientation(LinearLayout.VERTICAL);
        Button btn = new Button(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1140 * 2);
        btn.setBackgroundColor(Color.BLUE);
        btn.setLayoutParams(params);
        mScrollLinearlayoutView.addView(btn);

        Button btn2 = new Button(this);
        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1140 * 2);
        btn2.setBackgroundColor(Color.GREEN);
        btn2.setLayoutParams(params2);
        mScrollLinearlayoutView.addView(btn2);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "toas.btn", Toast.LENGTH_SHORT).show();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "toas.btn2", Toast.LENGTH_SHORT).show();

            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_left:
                Toast.makeText(getApplicationContext(),"left",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_right:
                Toast.makeText(getApplicationContext(),"right",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_all:
                Toast.makeText(getApplicationContext(),"btn_all",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_pull_to_list:
                mPullToZoonListView.setVisibility(View.VISIBLE);
                mScrollLinearlayoutView.setVisibility(View.GONE);
                break;
            case R.id.btn_scroll_linearlayout:
                mPullToZoonListView.setVisibility(View.GONE);
                mScrollLinearlayoutView.setVisibility(View.VISIBLE);
                break;

        }
    }


    class MyAdapter extends BaseAdapter {
        private LayoutInflater mLayoutInflater;
        public MyAdapter(Context context){
            mLayoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if(convertView == null){
                convertView = mLayoutInflater.inflate(R.layout.view_pull_to_zoon_listview_headview_child,parent,false);
                holder = new ViewHolder();
                holder.btnLeft = ((Button) convertView.findViewById(R.id.btn_left));
                holder.btnRight = ((Button) convertView.findViewById(R.id.btn_right));
                holder.btnAll = ((Button) convertView.findViewById(R.id.btn_all));
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            holder.btnLeft.setOnClickListener(PullToZoonActivity.this);
            holder.btnRight.setOnClickListener(PullToZoonActivity.this);
            holder.btnAll.setOnClickListener(PullToZoonActivity.this);
            return convertView;
        }

        class ViewHolder{
            public Button btnLeft;
            public Button btnRight;
            public Button btnAll;

        }
    }




}
